package cumeo89.chii.client.entry.renderer;

import static cumeo89.chii.client.entry.Constants.*;

import com.google.gwt.user.client.Window;
import com.google.gwt.xml.client.Attr;
import com.google.gwt.xml.client.Element;
import com.google.gwt.xml.client.Node;

import cumeo89.chii.client.entry.AbstractRenderer;
import cumeo89.chii.client.entry.Entry;
import cumeo89.chii.client.entry.Renderer;
import cumeo89.chii.client.entry.RendererFactory;
import cumeo89.chii.client.util.MyArrays;
import cumeo89.chii.client.util.XMLUtil;

public class DefaultRendererFactory implements RendererFactory {

	private static final String[] TRIVIAL_LIST = { E_ENTRY, E_SUBLIST,
			E_ITEM_LIST, E_LANG_LIST, A_POS_SPELL, };

	private static final String[] WIKITEXT = { A_POS_SPELL, A_DEF_CONTENT,
			A_EXAMPLE_CONTENT, A_EXAMPLE_EXPLAIN, "title" };

	private Entry entry;

	public DefaultRendererFactory(Entry entry) {
		this.entry = entry;
	}

	public Renderer getRenderer(Node node) {
		AbstractRenderer renderer = getAbstractRenderer(node);
		renderer.setEntry(entry);
		return renderer;
	}

	private AbstractRenderer getAbstractRenderer(Node node) {
		switch (node.getNodeType()) {
		case Node.ELEMENT_NODE: {
			Element element = (Element) node;
			String tag = element.getTagName();

			if (E_CATEGORY.equals(tag)) {
				return new TrivialListRenderer("span", "category",
						new String[] { A_CATEGORY_NAME });
			}
			if (E_INTERWIKI.equals(tag)) {
				return new TrivialListRenderer("span", "interwiki",
						new String[] { A_INTERWIKI_LANG });
			}
			if (E_CATEGORY_LIST.equals(tag) || E_INTERWIKI_LIST.equals(tag)) {
				return new SeperatedListRenderer();
			}
			if (E_SUBLIST.equals(tag)) {
				return new TwoColumnListRenderer();
			}
			if (E_POS.equals(tag)) {
				return new PosRenderer();
			}
			if (E_LANG.equals(tag)) {
				return new LanguageRenderer();
			}
			if (E_ITEM.equals(tag)) {
				return new ItemRenderer();
			}
			if (E_DEF.equals(tag)) {
				return new DefinitionRenderer();
			}
			if (E_CONTEXT_LIST.equals(tag)) {
				return new TrivialListRenderer("span", "context-list");
			}
			if (E_CONTEXT.equals(tag)) {
				return new TrivialListRenderer("span", "context");
			}
			if (E_EXAMPLE.equals(tag)) {
				return new ExampleRenderer();
			}
			if (E_DEF_LIST.equals(tag)) {
				return new TrivialListRenderer("ol", "list");
			}
			if (E_EXAMPLE_LIST.equals(tag)) {
				return new TrivialListRenderer("dl", "list");
			}
			if (E_EXPRESSION.equals(tag)) {
				return new TrivialListRenderer("li", "list");
			}
			if (MyArrays.contains(TRIVIAL_LIST, tag)) {
				return new TrivialListRenderer("div", "list");
			}
			if (MyArrays.contains(HeaderListRenderer.SUPPORTED_CODES, tag)) {
				return new HeaderListRenderer();
			}
			Window.alert("element: " + element.getTagName());
			break;
		}
		case Node.ATTRIBUTE_NODE: {
			String name = ((Attr) node).getName();
			String tag = XMLUtil.getElement((Attr) node).getTagName();
			if ("code".equals(name)) {
				return new CodeRenderer();
			}
			if (E_CONTEXT.equals(tag) && A_CONTEXT_TEXT.equals(name)) {
				return new TextRenderer("context");
			}
			if (E_INTERWIKI.equals(tag) && A_INTERWIKI_LANG.equals(name)) {
				return new InterwikiLanguageRenderer();
			}
			if (E_ITEM.equals(tag) && A_LANG.equals(name)) {
				return new ItemLanguageRenderer();
			}
			if (A_CATEGORY_NAME.equals(name)) {
				return new TextRenderer("category-name");
			}
			if (MyArrays.contains(WIKITEXT, name)) {
				return new WikitextRenderer();
			}
			Window.alert("attr: " + name);
			break;
		}
		case Node.TEXT_NODE:
			return new WikitextRenderer();
		}
		return null;
	}

}
